package com.wqr.mobile.android.ui.scrolltab;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.TabHost;
import android.widget.TextView;

import com.wqr.mobile.android.ui.AbsFragmentActivity;
import com.wqr.mobile.android.ui.R;

public class ScrollTab extends AbsFragmentActivity {

	TabHost tabHost;
	TabManager mTabManager;
	HashMap<String, TabItem> mTitles = new HashMap<String, TabItem>();
	public static final String ITEM[] = { "首页", "收藏", "历史", "搜狐新闻", "网易新闻", "腾讯", "阿里巴巴" };
	HorizontalScrollView mScrollView;

	@Override
	protected void setUp() {
		setContentView(R.layout.activity_scrolltabs);
		mScrollView = (HorizontalScrollView) findViewById(R.id.tabscontent);
		tabHost = (TabHost) findViewById(android.R.id.tabhost);
		tabHost.setup();

		mTabManager = new TabManager(this, tabHost, R.id.realtabcontent, mTitles);

		mTabManager.addTab(tabHost.newTabSpec(ITEM[0]).setIndicator(createTabView(ITEM[0])), TestFragment.class, putArgs(0));
		mTabManager.addTab(tabHost.newTabSpec(ITEM[1]).setIndicator(createTabView(ITEM[1])), TestFragment.class, putArgs(1));
		mTabManager.addTab(tabHost.newTabSpec(ITEM[2]).setIndicator(createTabView(ITEM[2])), TestFragment.class, putArgs(2));
		mTabManager.addTab(tabHost.newTabSpec(ITEM[3]).setIndicator(createTabView(ITEM[3])), TestFragment.class, putArgs(3));
		mTabManager.addTab(tabHost.newTabSpec(ITEM[4]).setIndicator(createTabView(ITEM[4])), TestFragment.class, putArgs(4));
		mTabManager.addTab(tabHost.newTabSpec(ITEM[5]).setIndicator(createTabView(ITEM[5])), TestFragment.class, putArgs(5));
		mTabManager.addTab(tabHost.newTabSpec(ITEM[6]).setIndicator(createTabView(ITEM[6])), TestFragment.class, putArgs(6));

		tabHost.setCurrentTab(0);
	}

	Bundle putArgs(int i) {
		Bundle b = new Bundle();
		b.putString("postion", "postion-->" + i);
		return b;
	}

	private View createTabView(String text) {
		View view = LayoutInflater.from(this).inflate(R.layout.layout_tab_indicator, null);
		TextView tv = (TextView) view.findViewById(R.id.tv_tab);
		tv.setText(text);
		TabItem item = new TabItem();
		item.title = tv;
		mTitles.put(text, item);
		return view;
	}

	@Override
	protected void destory() {
		// TODO Auto-generated method stub

	}

	class TabItem {
		public TextView title;
	}

	public static class TabManager implements TabHost.OnTabChangeListener {
		private final FragmentActivity mActivity;
		private final TabHost mTabHost;
		private final int mContainerId;
		private final HashMap<String, TabInfo> mTabs = new HashMap<String, TabInfo>();
		HashMap<String, TabItem> mTitles;
		TabInfo mLastTab;

		static final class TabInfo {
			private final String tag;
			private final Class<?> clss;
			private final Bundle args;
			private Fragment fragment;

			TabInfo(String _tag, Class<?> _class, Bundle _args) {
				tag = _tag;
				clss = _class;
				args = _args;
			}
		}

		static class DummyTabFactory implements TabHost.TabContentFactory {
			private final Context mContext;

			public DummyTabFactory(Context context) {
				mContext = context;
			}

			@Override
			public View createTabContent(String tag) {
				View v = new View(mContext);
				v.setMinimumWidth(0);
				v.setMinimumHeight(0);
				return v;
			}
		}

		public TabManager(FragmentActivity activity, TabHost tabHost, int containerId, HashMap<String, TabItem> titles) {
			mActivity = activity;
			mTabHost = tabHost;
			mContainerId = containerId;
			this.mTitles = titles;
			mTabHost.setOnTabChangedListener(this);
		}

		public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
			tabSpec.setContent(new DummyTabFactory(mActivity));
			String tag = tabSpec.getTag();

			TabInfo info = new TabInfo(tag, clss, args);

			// Check to see if we already have a fragment for this tab, probably
			// from a previously saved state. If so, deactivate it, because our
			// initial state is that a tab isn't shown.
			info.fragment = mActivity.getSupportFragmentManager().findFragmentByTag(tag);
			if (info.fragment != null && !info.fragment.isDetached()) {
				FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
				ft.detach(info.fragment);
				ft.commit();
			}

			mTabs.put(tag, info);
			mTabHost.addTab(tabSpec);
		}

		@Override
		public void onTabChanged(String tabId) {
			TabInfo newTab = mTabs.get(tabId);
			if (mLastTab != newTab) {
				FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
				ft.setCustomAnimations(R.anim.slide_up_in, R.anim.slide_down_out, R.anim.fadein, R.anim.fadeout);
				if (mLastTab != null) {
					if (mLastTab.fragment != null) {
						ft.detach(mLastTab.fragment);
					}
				}
				if (newTab != null) {
					if (newTab.fragment == null) {
						newTab.fragment = Fragment.instantiate(mActivity, newTab.clss.getName(), newTab.args);
						ft.add(mContainerId, newTab.fragment, newTab.tag);
					} else {
						ft.attach(newTab.fragment);
					}
				}

				mLastTab = newTab;
				ft.commit();
				mActivity.getSupportFragmentManager().executePendingTransactions();
				
			}

			for (Iterator iter = mTitles.entrySet().iterator(); iter.hasNext();) {
				Map.Entry entry = (Map.Entry) iter.next();
				String key = (String) entry.getKey();
				TabItem item = mTitles.get(key);
				if (key.equals(tabId)) {
					item.title.setTextColor(Color.parseColor("#fc7d21"));
				} else {
					item.title.setTextColor(Color.parseColor("#000000"));
				}
			}
		}
	}

}
